home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / pc / mac_file / vendor_d / neuralwa / nw2v50 / weathrio.c < prev    next >
Text File  |  1993-08-23  |  23KB  |  761 lines

  1. /* 17:00 04-Jul-88  (weathrio.c)  Weather Prediction with Madeline */
  2.  
  3. /************************************************************************
  4.  * Copyright(C) 1987-1992 NeuralWare Inc                                *
  5.  * Penn Center West, IV-227, Pittsburgh, PA 15276                       *
  6.  * Telephone: (412) 787-8222    FAX: (412) 787-8220                     *
  7.  *                                                                      *
  8.  * All rights reserved.  No part of this program may be reproduced,     *
  9.  * stored in a retrieval system, or transmitted, in any form or by any  *
  10.  * means, electronic, mechanical, photocopying, recording or otherwise  *
  11.  * without the prior written permission of the copyright owner,         *
  12.  * NeuralWare, Inc.                                                     *
  13.  *                                                                      *
  14.  *                          PROPRIETARY NOTICE                          *
  15.  *                                                                      *
  16.  * This document is the property of NeuralWare, Inc. and contains       *
  17.  * trade-secrets and other proprietary information.  The information    *
  18.  * herein is reserved as proprietary to NeuralWare, and is not to be    *
  19.  * published, reproduced, copied, disclosed, used, or reverse           *
  20.  * engineered without the express written consent of a duly authorized  *
  21.  * representative of NeuralWare.                                        *
  22.  ************************************************************************
  23.  */
  24.  
  25. #include "userutl.h"
  26.  
  27. #include <string.h>
  28. #include <math.h>
  29.  
  30. #ifndef SUN
  31. #ifndef DLC
  32. #include <stdlib.h>
  33. #endif
  34. #endif
  35.  
  36. #ifdef MAC
  37. #include  "macuio.redef"
  38. #endif
  39.  
  40. #if defined(_MSC_VER) || defined(__TURBOC__)
  41. int strcmpl( char *, char * );
  42. #endif
  43.  
  44. /************************************************************************
  45.  *
  46.  *  WEATHRIO - Weather Prediction User I/O
  47.  *
  48.  ************************************************************************
  49.  */
  50.  
  51. /* external routines */
  52. #ifdef PROTOTYPING
  53. /* --- prototypes --- */
  54. double  fabs( double );
  55. double  sqrt( double );
  56. #else
  57. double  fabs( );
  58. double  sqrt( );
  59. #endif
  60.  
  61. static short  nw_type;    /* type of network : */
  62. #define ADALINE   0
  63. #define OTHER   1
  64.  
  65.  
  66. /* local data declarations */
  67. static  float bp, dbp;    /* bar. pressure, delta b.p. */
  68. static  float prec_tod, prec_tom, prec_nxt; /* rainfall today, tomorrow..*/
  69. static  int wd, dwd;    /* wind direction, delta w. dir */
  70. static  char  date[12];
  71.  
  72. /* File definitions and declarations */
  73. #define MAX_STR 132       /* For reading from file */
  74. static  char  fbuf[3][MAX_STR + 1];   /* buffers for file i/o */
  75. static  int tod_buff, tom_buff, nxt_buff; /* buffer indices */
  76. static  int num_readings;     /* Actual # readings */
  77. static  short no_more_data;     /* recall flag */
  78. static  FILE  *fp = 0;
  79. static  FILE  *ifp = 0;
  80. static  long    start_pos;
  81. static  char  file_name[40];      /* larger for SUNs */
  82.  
  83.  
  84. /* Forecast parameters from user' file */
  85.  
  86. static float  prec_thr;   /* precipitation threshold */
  87. static float  bp_low, bp_high;  /* barometric pressure limits */
  88. static float  dbp_low, dbp_high;  /* delta barometric pressure limits */
  89. static float  wd_low, wd_high;  /* wind direction limits */
  90. static float  dwd_low, dwd_high;  /* delta wind direction limits */
  91.  
  92. /* Following need to be long for MAC scanfs */
  93. static long bp_npe;     /* # PEs for barometrics pressure */
  94. static long dbp_npe;    /* # PEs for delta bar. pressure */
  95. static long wd_npe;     /* # PEs for wind direction */
  96. static long dwd_npe;    /* # PEs for delta wind direction */
  97.  
  98. /* Network parameters */
  99. static  int   nlayp, ninp, noutp, ltype;  /* Network parameters */
  100. static  char  *csp, *netnp;     /* Network pointers*/
  101.  
  102.  
  103. /* graphics parameters */
  104. static  int xsize, ysize, ncolor, chrx, chry;
  105.  
  106.  
  107. /* Output display definitions and declarations for learning */
  108. static  double  tod_mse, tom_mse, nxt_mse;  /* Mean square errors */ 
  109.  
  110.  
  111. /* Output display definitions and declarations for recall */
  112. #define NUM_DISP  31      /* Maximum number displayed*/
  113. #define X_MARGIN  5
  114. #define Y_MARGIN  5
  115. static  int rstrtx, rstrty, rendx, rendy; /* Display window params */
  116. static  int ract_y, rtod_y, rtom_y, rnxt_y;/* y values for the 3 rows */
  117. static  int rdata;        /* start of data display */
  118. static  int curr_pos;     /* current data position */
  119. static  int dateline, actline;
  120.  
  121.  
  122. /* Results */
  123. static  float tod_correct, tom_correct, nxt_correct;
  124.  
  125. #ifndef DLC
  126. /* special routine to fill in for SUN386i */
  127. int strcmpl( pa, pb )
  128. char  *pa, *pb;   /* strings to compare */
  129. {
  130.     int   a, b;     /* work characters */
  131.     int   rc;     /* return code */
  132.  
  133.     for(;;) {
  134.   a = *pa++;
  135.   b = *pb++;
  136.   if ( 'A' <= a && a <= 'Z' ) a -= 'A'-'a';
  137.   if ( 'A' <= b && b <= 'Z' ) b -= 'A'-'a';
  138.   if ( (rc = a - b) != 0 ) return( rc );
  139.   if ( a == 0 ) return( 0 );
  140.     }
  141. }
  142. #endif
  143.  
  144. /* local routines */
  145.  
  146. /* --- prototypes --- */
  147.  
  148. void encode ARGLIST(( char * ));
  149. void decode ARGLIST(( void ));
  150. void li_code ARGLIST(( double, double, double, int, char * ));
  151. void disp_rwdw ARGLIST (( void ));
  152. void disp_forecast ARGLIST (( void ));
  153.  
  154.  
  155. void encode( buf )
  156. char  *buf;
  157. {
  158.   char  sbuf[100];
  159.   float denom, wf;
  160.   float nbp, ndbp, nwd, ndwd; /* Normalized data */
  161.  
  162.   if ( nw_type == ADALINE ) {
  163.     strcpy( buf, date );
  164.     strcat( buf, " " );
  165.   
  166.     li_code( bp, bp_low, bp_high, (int)bp_npe, sbuf );
  167.     strcat( buf, sbuf );
  168.     strcat( buf, " " );
  169.  
  170.     wf = (float) fabs( (double)dbp_low );
  171.     if ( wf < dbp_high ) wf = dbp_high;
  172.     li_code( (double) fabs((double)dbp), 0.0, wf, (int)(dbp_npe-1), sbuf );
  173.     strcat( buf, sbuf );
  174.     strcat( buf, " " );
  175.     if ( dbp < 0.0 ) strcat( buf, "0 " );
  176.     else strcat( buf, "1 " );
  177.  
  178.     li_code( (double) wd, wd_low, wd_high, (int)wd_npe, sbuf );
  179.     strcat( buf, sbuf );
  180.     strcat( buf, " " );
  181.  
  182.     li_code( (double) dwd, dwd_low, dwd_high, (int)dwd_npe, sbuf );
  183.     strcat( buf, sbuf );
  184.     strcat( buf, " " );
  185.  
  186.     strcat( buf, " " );
  187.     strcat( buf, (prec_tod > prec_thr) ? "1" : "0" );
  188.     strcat( buf, (prec_tom > prec_thr) ? "1" : "0" );
  189.     strcat( buf, (prec_nxt > prec_thr) ? "1" : "0" );
  190.   strcat( buf, NEW_LINE_STR );
  191.   } else {
  192.     denom = bp_high - bp_low;
  193.     if (denom >= 0.0001 ) nbp = ( bp - bp_low ) / denom;
  194.     else nbp = 0.0;
  195.  
  196.     denom = dbp_high - dbp_low;
  197.     if (denom >= 0.0001 ) ndbp = ( dbp - dbp_low ) / denom;
  198.     else ndbp = 0.0;
  199.  
  200.     denom = wd_high - wd_low;
  201.     if (denom >= 0.0001 ) nwd = ( wd - wd_low ) / denom;
  202.     else nwd = 0.0;
  203.  
  204.     denom = dwd_high - dwd_low;
  205.     if (denom >= 0.0001 ) ndwd = ( dwd - dwd_low ) / denom;
  206.     else ndwd = 0.0;
  207.  
  208.     sprintf( buf, "%s %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f %6.3f%s",
  209.        date, nbp, ndbp, nwd, ndwd, prec_tod, prec_tom, prec_nxt, NEW_LINE_STR );
  210.   }
  211. }
  212.  
  213.  
  214. #ifdef ANSI_HEADER
  215. void li_code( double val, double low, double high, int npe, char *sbuf )
  216. #else
  217. void li_code( val, low, high, npe, sbuf )
  218. double  val, low, high;
  219. int npe;
  220. char  *sbuf;
  221. #endif
  222. {
  223.   float increm;
  224.   int wx;
  225.   int pos = 0;
  226.   
  227.   strcpy( sbuf, "" );
  228.   if ( npe > 0 ) {
  229.     increm = ( high - low) / (float) npe;
  230.     if ( increm > 0.0001) pos = (int) ( (val - low) / increm);
  231.     if ( pos < 0 ) pos = 0;
  232.     if ( pos >= npe ) pos = npe - 1;
  233.     for ( wx = 0; wx < npe; wx++ )
  234.       strcat( sbuf, (wx == pos) ? "1" : "0" );
  235.   }
  236. }
  237.  
  238. void decode( )
  239. {
  240.   char  wc; /* work character */
  241.   float *pio; /* pointer to IODATA */
  242.   int wx;
  243.   char  *sp;
  244.  
  245.   if ( nw_type == ADALINE ) {
  246.     pio = IODATA;
  247.     sp  = fbuf[0];
  248.  
  249.     sscanf( sp, "%s", date );
  250.     sp += strlen( date );
  251.  
  252.     for ( wx = 0; wx < ninp; wx++ ) {
  253.       do  sscanf( sp++, "%1c", &wc );
  254.       while ( wc == ' ' ) ;
  255.       if ( wc == '0' ) *pio++ = -1.0;
  256.       else *pio++ = 1.0;
  257.     }
  258.     do  sscanf( sp++, "%1c", &wc );
  259.     while ( wc == ' ' ) ;
  260.     if ( wc == '0' ) prec_tod = -1.0;
  261.     else prec_tod = 1.0;
  262.     do  sscanf( sp++, "%1c", &wc );
  263.     while ( wc == ' ' ) ;
  264.     if ( wc == '0' ) prec_tom = -1.0;
  265.     else prec_tom = 1.0;
  266.     do  sscanf( sp++, "%1c", &wc );
  267.     while ( wc == ' ' ) ;
  268.     if ( wc == '0' ) prec_nxt = -1.0;
  269.     else prec_nxt = 1.0;
  270.   } else {
  271.     sscanf( fbuf[0], "%s %f %f %f %f %f %f %f",
  272.       date, &IODATA[0], &IODATA[1], &IODATA[2], &IODATA[3],
  273.       &prec_tod, &prec_tom, &prec_nxt );
  274.   }
  275. }
  276.  
  277. void disp_rwdw( )   /* Display recall window */
  278. {
  279.   ug_window( 1, gm_intcolor, rstrtx, rstrty, rstrtx + rendx, rstrty + rendy );
  280.   ug_box( 1, gm_outcolor, 0, 0, rendy, rendx, rendy, 0 );
  281.   ug_line( 1, gm_outcolor, 0, rdata, 0, rdata, rendy , 0 );
  282.   ug_line( 1, gm_outcolor, 0, 0, dateline, rendx, dateline , 0 );
  283.   ug_line( 1, gm_outcolor, 0, 0, actline, rendx, actline , 0 );
  284.   ug_puts( 1, gm_txtcolor, 0, X_MARGIN, rtod_y, "Today:", 0 );
  285.   ug_puts( 1, gm_txtcolor, 0, X_MARGIN, ract_y, "Actual:", 0 );
  286.   ug_puts( 1, gm_txtcolor, 0, X_MARGIN, rtom_y, "Tomorrow:", 0 );
  287.   ug_puts( 1, gm_txtcolor, 0, X_MARGIN, rnxt_y, "Next Day:", 0 );
  288. }
  289.  
  290. void disp_forecast( )
  291. {
  292.   int size, x0;
  293.   
  294.   x0 = rdata + 2 + curr_pos * ( chry + 2 );
  295.    
  296.   size = IODATA[0] * chry;
  297.   if ( size < 1 ) size = 1;
  298.   if ( size > chry ) size = chry;
  299.   ug_boxf( 1, gm_txtcolor, 0, x0, rtod_y, x0 + size - 1, rtod_y + size - 1 );
  300.  
  301.   size = prec_tod * chry;
  302.   if ( size < 1 ) size = 1;
  303.   if ( size > chry ) size = chry;
  304.   ug_boxf( 1, gm_txtcolor, 0, x0, ract_y, x0 + size - 1, ract_y + size - 1 );
  305.  
  306.   ug_puts( 1, gm_txtcolor, 0, x0 + chry + 1, dateline + 2, date, 1 );
  307.  
  308.   x0 += chry+2;
  309.   size = IODATA[1] * chry;
  310.   if ( size < 1 ) size = 1;
  311.   if ( size > chry ) size = chry;
  312.   ug_boxf( 1, gm_txtcolor, 0, x0, rtom_y, x0 + size - 1, rtom_y + size - 1 );
  313.  
  314.   x0 += chry+2;
  315.   size = IODATA[2] * ( chry );
  316.   if ( size < 1 ) size = 1;
  317.   if ( size > chry ) size = chry;
  318.   ug_boxf( 1, gm_txtcolor, 0, x0, rnxt_y, x0 + size - 1, rnxt_y + size - 1 );
  319.    
  320. }
  321.  
  322.  
  323. /************************************************************************
  324.  *
  325.  *  UsrIO - user I/O routine to handle requests from NWORKS
  326.  *
  327.  ************************************************************************
  328.  */
  329.  
  330. static int AbortFlag  = 0;
  331. static int InitFlag = 0;
  332.  
  333. int UsrIO()     /* handle NWORKS requests */
  334. {
  335.     int   wx;       /* work indices */
  336.     int   key, xp, yp, button;      /* mouse interface */
  337.     char  *sp;          /* string pointer */
  338.     long   total_npe;       /* Total # PEs */
  339.     int   oldwd;
  340.     float oldbp;
  341.     char  sbuf[100];        /* string buffer */
  342.  
  343.     IORTNCDE = 0;       /* good return for data */
  344.     
  345.     if ( InitFlag == 0 ) {
  346.       /* Read network parameters */
  347.       ug_rdnetinf( &nlayp, &ninp, &noutp, <ype, &csp, &netnp );
  348.       /* strcpy( file_name, netnp ); */
  349.       strcpy( file_name, "june.dat" );
  350.  
  351.       /* Get graphics parameters */
  352.       ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry );
  353.       
  354.       if ( ncolor < 8 ) {
  355.   gm_intcolor = 1;
  356.   gm_txtcolor = 0;
  357.   gm_outcolor = 0;
  358.       } else {
  359.   gm_intcolor = 7;
  360.   gm_txtcolor = 4;
  361.   gm_outcolor = 0;
  362.       }
  363.  
  364.       ract_y = Y_MARGIN;
  365.       actline = ract_y + chry + 2;
  366.       rnxt_y = ract_y + Y_MARGIN + chry + 1;
  367.       rtom_y = rnxt_y + Y_MARGIN + chry;
  368.       rtod_y = rtom_y + Y_MARGIN + chry;
  369.       dateline = rtod_y + chry + Y_MARGIN + 1;
  370.       rendy = dateline + 8 * chrx + 2 * Y_MARGIN + 1;
  371.       rdata = 9 * chrx + 2 * X_MARGIN + 2;
  372.       rendx = rdata + 2 * X_MARGIN + ( chry + 2 ) * NUM_DISP + 1;
  373.  
  374.       rstrtx = ( xsize - rendx ) / 2;
  375.       rstrty = ( ysize - rendy ) / 2;
  376.       InitFlag = 1;
  377.     }
  378.     
  379.     
  380.     switch( IOREQCDE ) {
  381.     case RQ_ATTENTION:
  382.   /* This is invoked at the request of the user from the
  383.      execute menu.  It allows parameters to be changed or
  384.      altered.  Any graphics or other interactions are allowable
  385.      as usual for the other options.
  386.   */
  387.       if ( ifp != (FILE *) NULL ) fclose( ifp );
  388.       if ( fp != (FILE *) NULL ) fclose( fp );
  389.  
  390.       ug_rdnetinf( &nlayp, &ninp, &noutp, <ype, &csp, &netnp );
  391.       if ( strcmpl( csp, "Adaline") == 0 ) nw_type = ADALINE;
  392.       else nw_type = OTHER;
  393.  
  394.       PutStr(
  395.  "This will process your weather file to a form acceptable by the network\n");
  396.       /* Read weather file */
  397.       PutStr("Enter name of weather data file:");
  398.       sp = GetStr( );
  399.  
  400.       if ( ( fp = fopen( sp, "r" ) ) == NULL) {
  401.   sprintf( sbuf, "Cannot find file <%s>\n", sp );
  402.   PutStr( sbuf );
  403.   goto bad_return;
  404.       }
  405.  
  406.       PutStr("Enter name of transformed file:");
  407.       sp = GetStr( );
  408.  
  409. #if !defined(MAC) && !defined(VMS)
  410.       unlink( sp ); /* delete if it already exists */
  411. #endif
  412.        if ( ( ifp = fopen( sp, "w" ) ) == NULL) {
  413.    sprintf( sbuf, "Cannot open file <%s>\n", sp );
  414.    PutStr( sbuf );
  415.    goto bad_return;
  416.       }
  417.       strcpy( file_name, sp );
  418.  
  419.  
  420.       /****************************************/
  421.       /* Read in weather parameters from file */
  422.       /****************************************/
  423.       if ( fgets( fbuf[0], MAX_STR, fp) == 0 ) {
  424.   PutStr("Could not read precipitation threshold; i/o aborted\n");
  425.   goto bad_return;
  426.       }
  427.       sscanf( fbuf[0], "%f", &prec_thr );
  428.  
  429.       if ( fgets( fbuf[0], MAX_STR, fp) == 0 ) {
  430.   PutStr(
  431.       "Could not read barometric pressure parameters; i/o aborted\n");
  432.   goto bad_return;
  433.       }
  434.       sscanf( fbuf[0], "%f %f %ld",
  435.        &bp_low, &bp_high, &bp_npe );
  436.               
  437.       if ( fgets( fbuf[0], MAX_STR, fp) == 0 ) {
  438.   PutStr(
  439.       "Could not read delta barometric pressure parameters; i/o aborted\n");
  440.   goto bad_return;
  441.       }
  442.       sscanf( fbuf[0], "%f %f %ld",
  443.        &dbp_low, &dbp_high, &dbp_npe );
  444.  
  445.       if ( fgets( fbuf[0], MAX_STR, fp) == 0 ) {
  446.   PutStr("Could not read weather direction parameters; i/o aborted\n");
  447.   goto bad_return;
  448.       }
  449.       sscanf( fbuf[0], "%f %f %ld",
  450.        &wd_low, &wd_high, &wd_npe );
  451.               
  452.       if ( fgets( fbuf[0], MAX_STR, fp) == 0 ) {
  453.   PutStr(
  454.       "Could not read delta weather direction parameters; i/o aborted\n");
  455.   goto bad_return;
  456.       }
  457.       sscanf( fbuf[0], "%f %f %ld",
  458.        &dwd_low, &dwd_high, &dwd_npe );
  459.  
  460.       if ( nw_type == ADALINE )
  461.   total_npe = bp_npe + dbp_npe + wd_npe + dwd_npe;
  462.       else total_npe = 4;
  463.     
  464.       sprintf(fbuf[0], "%ld %f    ! Number PEs, precipitation threshold%s",
  465.           total_npe, prec_thr,NEW_LINE_STR );
  466.       fputs( fbuf[0], ifp );
  467.       
  468.       /****************************************************************/
  469.       /* Read in weather data from file, convert, write to input file */
  470.       /****************************************************************/
  471.       tod_buff = 0;
  472.       tom_buff = 1;
  473.       nxt_buff = 2;
  474.       /* Read in first three lines of data */
  475.       if ( fgets( fbuf[tod_buff], MAX_STR, fp ) == 0 ) {
  476.   PutStr(" Insufficient data! ");
  477.   goto bad_return;
  478.       }
  479.       if ( fgets( fbuf[tom_buff], MAX_STR, fp ) == 0 ) {
  480.   PutStr(" Insufficient data! ");
  481.   goto bad_return;
  482.       }
  483.       if ( fgets( fbuf[nxt_buff], MAX_STR, fp ) == 0 ) {
  484.   PutStr(" Insufficient data! ");
  485.   goto bad_return;
  486.       }
  487.       sscanf( fbuf[nxt_buff], "%s %f %ld %f", date, &bp, &wd, &prec_nxt );
  488.       sscanf( fbuf[tom_buff], "%s %f %ld %f", date, &bp, &wd, &prec_tom );
  489.       sscanf( fbuf[tod_buff], "%s %f %ld %f", date, &bp, &wd, &prec_tod );
  490.       dbp = 0.0;
  491.       dwd = 0;
  492.       encode( fbuf[tod_buff] );
  493.       fputs( fbuf[tod_buff], ifp );
  494.       oldbp = bp;
  495.       oldwd = wd;
  496.  
  497.       while ( fgets(fbuf[tod_buff],MAX_STR,fp) != 0 ) {
  498.   wx = tod_buff;      /* rotate buffer indices */
  499.   tod_buff = tom_buff;
  500.   tom_buff = nxt_buff;
  501.   nxt_buff = wx;
  502.   sscanf( fbuf[nxt_buff], "%s %f %ld %f", date, &bp, &wd, &prec_nxt );
  503.   sscanf( fbuf[tom_buff], "%s %f %ld %f", date, &bp, &wd, &prec_tom );
  504.   sscanf( fbuf[tod_buff], "%s %f %ld %f", date, &bp, &wd, &prec_tod );
  505.   dbp = bp - oldbp;
  506.   dwd = wd - oldwd;
  507.   if ( dwd > 180 ) dwd -= 360;
  508.   else if ( dwd <= -180 ) dwd += 360;
  509.   encode( fbuf[tod_buff] );
  510.   fputs( fbuf[tod_buff], ifp );
  511.   oldbp = bp;
  512.   oldwd = wd;
  513.       }
  514.       
  515.       fclose( fp );
  516.       fclose( ifp );
  517.       PutStr( "Data successfully transformed\n" );
  518.       return(0);
  519.  
  520. bad_return:
  521.       IORTNCDE = -1;
  522.       if ( ifp != (FILE *) NULL ) fclose( ifp );
  523.       if ( fp != (FILE *) NULL ) fclose( fp );
  524.  
  525.       break;
  526.  
  527.     case RQ_LSTART:       /* starting learn */
  528.   /* This tells the user that the program is about to start
  529.      learning.  It is called once for a LEARN ALL, LEARN ONE,
  530.      LEARN N, or LEARN START
  531.   */
  532.     case RQ_RSTART:       /* starting recall */
  533.   /* This tells the user that the program is about to start
  534.      a recall.  It is called once for a RECALL ALL, RECALL ONE,
  535.      RECALL N, or RECALL START.
  536.   */
  537.  
  538.       AbortFlag = 0;
  539.       /* Read network parameters */
  540.       ug_rdnetinf( &nlayp, &ninp, &noutp, <ype, &csp, &netnp );
  541.       if ( strcmpl( csp, "Adaline") == 0 ) nw_type = ADALINE;
  542.       else nw_type = OTHER;
  543.  
  544.       /* Read weather file */
  545.       if ( ifp != (FILE *) NULL ) fclose( ifp );
  546.       sprintf( sbuf, "Enter name of input file ( default: %s ):", file_name );
  547.       PutStr( sbuf );
  548.       sp = GetStr();
  549.       PutStr("\n");
  550.       if ( strcmp( sp, "" ) != 0 ) strcpy( file_name, sp );
  551.  
  552.       if ( ( ifp = fopen( file_name, "r" ) ) == NULL ) {
  553.   sprintf( sbuf, "Cannot find file <%s>\n", file_name );
  554.   PutStr( sbuf );
  555.   goto abort;
  556.       }
  557.  
  558.       /* Read in header */
  559.         if ( fgets( fbuf[0], MAX_STR, ifp ) == 0 ) {
  560.   PutStr("Cannot read header; i/o aborted\n");
  561.   goto abort;
  562.       }
  563.       sscanf( fbuf[0], "%ld %f", &total_npe, &prec_thr );
  564.  
  565.      if ( total_npe != ninp ) {
  566.        PutStr( "Mismatch between network and input file; i/o aborted\n");
  567.        sprintf( sbuf,
  568.    "Number of network inputs = %ld, number of file inputs = %ld\n",
  569.     (long)ninp, total_npe );
  570.        PutStr( sbuf );
  571.        goto abort;
  572.      }
  573.  
  574.      num_readings = 0;
  575.      if ( IOREQCDE == RQ_RSTART ) {
  576.        disp_rwdw( );
  577.        curr_pos = 0;
  578.        no_more_data = 0;
  579.        tod_correct = tom_correct = nxt_correct = 0.0;
  580.      }
  581.      else {
  582.        start_pos = ftell( ifp );  /* start position for file data */
  583.        tod_mse = tom_mse = nxt_mse = 0.0; /* Initialize mean square errors */
  584.      }
  585.      return(0);
  586.      
  587. abort:
  588.      IORTNCDE = -1;
  589.      AbortFlag = 1;
  590.      if ( ifp != (FILE *) NULL ) fclose( ifp );
  591.  
  592.      break;
  593.  
  594.     case RQ_LEARNIN:        /* read training input */
  595.   /* IODATA points to an empty array of IOCOUNT elements.  The
  596.      values placed in this array by the user will become the
  597.      inputs to the network for training purposes.
  598.   */
  599.       if ( AbortFlag ) {
  600.   IORTNCDE = -1;
  601.   return(0);
  602.       }
  603.       num_readings++;
  604.       if ( fgets( fbuf[0], MAX_STR, ifp ) == 0 ) {
  605.   fseek( ifp, start_pos, 0 ); /* If at end return to start */
  606.   fgets( fbuf[0], MAX_STR, ifp );
  607.   sprintf ( sbuf,
  608.     "Mean square error. today: %5.3f, tomorrow: %5.3f, next day: %5.3f\n",
  609.       sqrt(tod_mse) / num_readings,
  610.       sqrt(tom_mse) / num_readings,
  611.       sqrt(nxt_mse) / num_readings );
  612.   PutStr( sbuf );
  613.   tod_mse = tom_mse = nxt_mse = 0.0;
  614.   num_readings = 0;
  615.       }
  616.       decode( );
  617.       break;
  618.       
  619.     case RQ_READ:       /* read test data */
  620.   /* IODATA points to an empty array of IOCOUNT values.  The
  621.      user must fill in these values.  The elements of the
  622.      array will become the "sum" of the inputs to the input
  623.      layer of processing elements.
  624.   */ 
  625.       /* Read next line of data from file */
  626.  
  627.       if ( AbortFlag ) {
  628.   IORTNCDE = -1;
  629.   return;
  630.       }
  631.  
  632.       if ( fgets( fbuf[0], MAX_STR, ifp ) != 0 ) {
  633.   decode( );
  634.   num_readings++;
  635.       }
  636.       else no_more_data = 1;
  637.       break;
  638.  
  639.     case RQ_LEARNOUT:       /* read desired output */
  640.   /* IODATA points to an empty array of IOCOUNT values.  The
  641.      elements of the array will become the desired outputs for
  642.      training purposes.  These desired outputs correspond to
  643.      the most recent "RQ_LEARNIN" request.
  644.   */
  645.     case RQ_RCLTST:   /* read desired output during recall test */
  646.   /* IODATA points to an empty array of IOCOUNT values.  The
  647.      elements of the array will become the desired outputs for
  648.      recall purposes.  This request is only made during a
  649.      Execute Network/Recall Test.
  650.   */
  651.  
  652.   IODATA[0] = prec_tod;
  653.   IODATA[1] = prec_tom;
  654.   IODATA[2] = prec_nxt;
  655.         break;
  656.  
  657.       
  658.     case RQ_WRSTEP:       /* write interim results */
  659.   /* each recall cycle for the Hopfield and BAM control strategies,
  660.      the intermediate output is made available to userio to test
  661.      for convergence or other desired states.  This option is
  662.      not used for other control strategies.
  663.   */
  664.   if (AbortFlag) {
  665.     IORTNCDE = -1;
  666.     return;
  667.   } 
  668.   break;
  669.  
  670.  
  671.     case RQ_LEARNRSLT:
  672.   /* IODATA points to an array of IOCOUNT values.  These are the
  673.      output of the network caused by the inputs from RQ_LEARNIN.
  674.   */
  675.   if (AbortFlag) {
  676.     IORTNCDE = -1;
  677.     return;
  678.   } 
  679.   tod_mse += ( IODATA[0] - prec_tod ) * ( IODATA[0] - prec_tod );
  680.   tom_mse += ( IODATA[1] - prec_tom ) * ( IODATA[1] - prec_tom );
  681.   nxt_mse += ( IODATA[2] - prec_nxt ) * ( IODATA[2] - prec_nxt );
  682.   break;
  683.  
  684.     case RQ_WRITE:        /* write out results */
  685.   /* IODATA points to an array of IOCOUNT "float" type values.
  686.      The values are the outputs of the top-most layer of the
  687.      network.
  688.   */
  689.  
  690.   if (AbortFlag) {
  691.     IORTNCDE = -1;
  692.     return;
  693.   } 
  694.  
  695.   if ( no_more_data ) {
  696.     PutStr( "No more data; press mouse button to quit.\n" );
  697.     sprintf( sbuf,
  698.  "Percentage correct: today: %5.1f, tomorrow: %5.1f, next day: %5.1f\n",
  699.              ( tod_correct / num_readings ) * 100.0,
  700.              ( tom_correct / num_readings ) * 100.0,
  701.              ( nxt_correct / num_readings ) * 100.0 );
  702.     PutStr( sbuf );
  703.     for ( ; ; ) {
  704.       ug_mouse( &key, &xp, &yp, &button );
  705.       if ( button == MBUT_RIGHT || button == MBUT_LEFT ) {
  706.         IORTNCDE = -1;
  707.         goto end_write;
  708.       }
  709.     }
  710.         } else {
  711.     if ( (IODATA[0] >= prec_thr && prec_tod >= prec_thr) ||
  712.          (IODATA[0] < prec_thr && prec_tod < prec_thr) )
  713.       tod_correct += 1.0;
  714.     if ( (IODATA[1] >= prec_thr && prec_tom >= prec_thr) ||
  715.          (IODATA[1] < prec_thr && prec_tom < prec_thr) )
  716.       tom_correct += 1.0;
  717.     if ( (IODATA[2] >= prec_thr && prec_nxt >= prec_thr) ||
  718.          (IODATA[2] < prec_thr && prec_nxt < prec_thr) )
  719.       nxt_correct += 1.0;
  720.     disp_forecast( );
  721.     if ( ++curr_pos >= NUM_DISP ) {
  722.       curr_pos = 0;
  723. #ifdef MAC
  724.       PutStr(
  725.         "Press mouse button to continue; clover mouse button to abort.\n" );
  726. #else
  727.       PutStr(
  728.         "Press right button to abort, left button to continue.\n" );
  729. #endif
  730.       for ( ; ; ) {
  731.         ug_mouse( &key, &xp, &yp, &button );
  732.         if ( button == MBUT_RIGHT ) {
  733.           IORTNCDE = -1;
  734.           goto end_write;
  735.         }
  736.         if ( button == MBUT_LEFT  ) {
  737.     disp_rwdw( ); /* Redisplay recall window */
  738.     goto end_write;
  739.         }
  740.       }
  741.     }
  742.         }
  743. end_write:
  744.   break;
  745.  
  746.     case RQ_TERM:       /* terminate interface */
  747.   /* close any files which may be open.  Deallocate any memory
  748.      which was allocated.  (This is VERY VERY important.  If
  749.      allocated memory is NOT released, dos memory will become
  750.      fragmented and it will become necessary to reboot.
  751.   */
  752.  
  753.   PutStr( "bye bye\n" );
  754.   if ( ifp != (FILE *) NULL ) fclose( ifp );
  755.   if ( fp != (FILE *) NULL ) fclose( fp );
  756.   break;
  757.     }
  758.  
  759.     return;
  760. }
  761.